# - this module looks for Matlab
# Defines:
#  MATLAB_INCLUDE_DIR: include path for mex.h, engine.h
#  MATLAB_LIBRARIES:   required libraries: libmex, etc
#  MATLAB_MEX_LIBRARY: path to libmex.lib
#  MATLAB_MX_LIBRARY:  path to libmx.lib
#  MATLAB_ENG_LIBRARY: path to libeng.lib
#  MATLAB_LIBRARY_DIR: Library path

SET(MATLAB_FOUND 0)
SET(_MATLAB_INCLUDE_SEARCH_PATHS "")
SET(_MATLAB_ROOT "")
SET(_MATLAB_MEX_LIB "mex")
SET(_MATLAB_MX_LIB "mx")
SET(_MATLAB_ENG_LIB "eng")

IF(WIN32)
	IF(${CMAKE_GENERATOR} MATCHES "Visual Studio 6")
		SET(_MATLAB_ROOT "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MathWorks\\MATLAB\\7.0;MATLABROOT]/extern/lib/win32/microsoft/msvc60")
	ELSE(${CMAKE_GENERATOR} MATCHES "Visual Studio 6")
		IF(${CMAKE_GENERATOR} MATCHES "Visual Studio 7")
			# Assume people are generally using 7.1,
			# if using 7.0 need to link to: ../extern/lib/win32/microsoft/msvc70
			SET(_MATLAB_ROOT "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MathWorks\\MATLAB\\7.0;MATLABROOT]/extern/lib/win32/microsoft/msvc71")
		ELSE(${CMAKE_GENERATOR} MATCHES "Visual Studio 7")
			IF(${CMAKE_GENERATOR} MATCHES "Borland")
				# Same here, there are also: bcc50 and bcc51 directories
				SET(_MATLAB_ROOT "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MathWorks\\MATLAB\\7.0;MATLABROOT]/extern/lib/win32/microsoft/bcc54")
			ELSE(${CMAKE_GENERATOR} MATCHES "Borland")
				IF(MATLAB_FIND_REQUIRED)
					MESSAGE(FATAL_ERROR "Generator not compatible: ${CMAKE_GENERATOR}")
				ENDIF(MATLAB_FIND_REQUIRED)
			ENDIF(${CMAKE_GENERATOR} MATCHES "Borland")
		ENDIF(${CMAKE_GENERATOR} MATCHES "Visual Studio 7")
	ENDIF(${CMAKE_GENERATOR} MATCHES "Visual Studio 6")
		
	SET(_MATLAB_MEX_LIB "libmex")
	SET(_MATLAB_MX_LIB "libmx")
	SET(_MATLAB_ENG_LIB "libeng")

	SET( _MATLAB_INCLUDE_SEARCH_PATHS 
		"[HKEY_LOCAL_MACHINE\\SOFTWARE\\MathWorks\\MATLAB\\7.0;MATLABROOT]/extern/include"
	)
ENDIF( WIN32 )

IF( UNIX )
	IF( APPLE )
		SET(_MATLAB_ROOT
			$ENV{MATLABROOT}/bin/maci
		)
	ELSE( APPLE )
		IF(CMAKE_SIZEOF_VOID_P EQUAL 4)
			# Regular x86
			SET(_MATLAB_ROOT
				$ENV{MATLABROOT}/bin/glnx86/
			)
		ELSE(CMAKE_SIZEOF_VOID_P EQUAL 4)
			# AMD64:
			SET(_MATLAB_ROOT
				$ENV{MATLABROOT}/bin/glnxa64/
			)
		ENDIF(CMAKE_SIZEOF_VOID_P EQUAL 4)
	ENDIF( APPLE )
	
	SET( _MATLAB_INCLUDE_SEARCH_PATHS 
		$ENV{MATLABROOT}/extern/include/
	)
ENDIF( UNIX )

# Find the actual libraries and include directory
FIND_LIBRARY(MATLAB_MEX_LIBRARY
	NAMES ${_MATLAB_MEX_LIB}
	PATHS ${_MATLAB_ROOT}
)
message(STATUS "MATLAB_MEX_LIBRARY = ${MATLAB_MEX_LIBRARY}")

FIND_LIBRARY(MATLAB_MX_LIBRARY
	NAMES ${_MATLAB_MX_LIB}
	PATHS ${_MATLAB_ROOT}
)
  
FIND_LIBRARY(MATLAB_ENG_LIBRARY
	NAMES ${_MATLAB_ENG_LIB}
	PATHS ${_MATLAB_ROOT}
)
	
FIND_PATH(MATLAB_INCLUDE_DIR
	NAMES mex.h
	PATHS ${_MATLAB_INCLUDE_SEARCH_PATHS}
)

FIND_PATH(MATLAB_LIBRARY_DIR
	NAMES ${_MATLAB_MEX_LIB}
	PATHS ${_MATLAB_ROOT}
)

# This is common to UNIX and Win32:
SET(MATLAB_LIBRARIES
  ${MATLAB_MEX_LIBRARY}
  ${MATLAB_MX_LIBRARY}
  ${MATLAB_ENG_LIBRARY}
)

IF(MATLAB_INCLUDE_DIR AND MATLAB_LIBRARIES)
  SET(MATLAB_FOUND 1)
ENDIF(MATLAB_INCLUDE_DIR AND MATLAB_LIBRARIES)

MARK_AS_ADVANCED(
  MATLAB_LIBRARIES
  MATLAB_MEX_LIBRARY
  MATLAB_MX_LIBRARY
  MATLAB_ENG_LIBRARY
  MATLAB_INCLUDE_DIR
  MATLAB_FOUND
  MATLAB_ROOT
)

